/*********************************************************************
 * Módulo 3. Tutorial de desarrollo con NME. Clase Enemigo.
 * Programación Multimedia y Juegos
 * Autor: David Vallejo Fernández    david.vallejo@tegnix.com
 * Autor: Carlos González Morcillo   carlos.gonzalez@tegnix.com
 *
 * You can redistribute and/or modify this file under the terms of the
 * GNU General Public License ad published by the Free Software
 * Foundation, either version 3 of the License, or (at your option)
 * and later version. See <http://www.gnu.org/licenses/>.
 *
 * This file is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.  
 *********************************************************************/

package graphics.sprites;

import graphics.ParticleSource;
import aze.display.TileLayer;

class Enemy extends NonPlayer {
  var _speed:Int;                  // Velocidad de desplazamiento
  var _particles:ParticleSource;   // Fuente de partículas

  // Constructor =====================================================
  public function new(id:String, layer:TileLayer,  w:Int, h:Int, 
		      spw:Int, sph:Int, speed:Int) {
    super(id, layer, w, h);  _speed = speed ;
    y = Std.int(sph/2.0 + Std.random(Std.int(h - sph)));
    x = Std.int(w + spw);
    _particles = new ParticleSource("smoke", _root, x, y, Linear);
  }

  // Métodos sobreescritos en Enemy ==================================
  override function isInside():Bool {
    return (x > -800);   // Para actualizar sistema de partículas
  }

  public override function removeChildObjects():Void {
    _particles.clearParticles();
  }

  // Actualización de partículas =====================================
  function updateParticles(w:Int, h:Int, eTime:Int) {
    _particles.setPosition(x+width/2.0,y);
    _particles.update(w, h, eTime);
    if (_particles.getNParticles() == 0) {
      _particles.addParticles(50, 100);
    }

    if (isInside() == false) removeChildObjects();
  }

  // Actualización del enemigo =======================================
  public override function update(w:Int, h:Int, eTime:Int):Bool {
    x-= _speed;
    updateParticles(w,h,eTime);

    return (isInside());
  }

}